/**
 * $Id: ReciverDeliverThread.java,v 1.14 2006/10/30 08:39:25 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.Queue;
import com.haoxi.gateway.connector.Connector;

/**
 * 下发所有
 * 
 * Revision History
 * 
 * 2006-5-25,star.Zhao,created it
 */
public class ReciverDeliverThread implements IRun {
    private static final Log log = LogFactory
            .getLog(ReciverDeliverThread.class);
    private Connector conn;
    private static Queue queue;
    private boolean running = false;
    private Date createTime;
    private Date lastRunTime;
    private int run_stat = IRun.RUNNER_READY;
    private String name;

    public ReciverDeliverThread(Connector con, Queue toQueue) {
        this.conn = con;

        queue = toQueue;
        createTime = new Date();
        setName("ReciverThread");
    }

    public void run() {
        log.debug("IRun: ReciverThread start !!!");

        // check conn
        if (conn == null) {
            return;
        }
        running = true;
        lastRunTime = new Date();

        Object obj;
        while (running) {
            log.debug("ReciverDeliverThread-------------------------------------:");
            lastRunTime.setTime(System.currentTimeMillis());
            obj = null;
            try {
                // get message
//                if (conn.isSocketKeepLive()) {
                    obj = conn.readProtcol();
//                }

//                else {
//                    synchronized (conn) {
//                        log
//                                .error(conn.getConnName()
//                                        + " is closed, current reciveDeliver thread wait !!!");
//                        conn.wait();
//                        conn.reOpenConnect();
//                        log.error("Wait " + conn.getConnName()
//                                + " is to be notified...-------");
//                        continue;
//                    }
//                }
                // receive obj
                // body:com.haoxi.gateway.common.CommonMsgBean@149d886
                if (obj != null) {

                    // add to union queue
                    log.debug("receive obj body:" + obj);
                    boolean bOK = queue.add(obj);
                    if (!bOK) {
                        log.error("ReciveQueue is full, add failure !!!");
                    }

                }
            } catch (IOException e) {
                log.error("IOException", e);
            } catch (Exception e) {
                log.error("other Exception", e);
            }
        }// end of while
        running = false;
        run_stat = IRun.RUNNER_DEAD;

        log.debug("IRun: ReciverThread ended !!!");
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public Date getLastRunTime() {
        return this.lastRunTime;
    }

    public boolean isRunning() {
        return running;
    }

    public void setStop() {
        running = false;
    }

    public int getStatus() {
        return this.run_stat;
    }

    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }

    /**
     * @param name The name to set.
     */
    public void setName(String name) {
        this.name = name;
    }

    public long getSleepTime() {
        return 0;
    }

    public void setSleepTime(long sleepTime) {

    }
}
